﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using API.Models;
using PPG.DataAccess;
using PPG.CryptoProvider;
using System.Xml;
using System.Data;
using System.Configuration;
using System.Web;

namespace API.Controllers
{
    public class NewUserController : ApiController
    {
        // <summary>
        /// Intializing Helper Classes - DB Connection and Security Provider for Encrypting and Decrypting Passwords
        /// Pass in DBServerName, DBUsern
        /// ame and DBPassword from Config File for DB connections
        /// The Security helper does nto take any parameters
        /// </summary>
        DataSourceCommunicator dsComm = new DataSourceCommunicator(DataSourceCommunicator.ParamType.ServerCredentials, ConfigurationManager.AppSettings["DBServerName"].ToString(), ConfigurationManager.AppSettings["DBUserName"].ToString(), ConfigurationManager.AppSettings["DBPassword"].ToString());
        CryptoAgent securityHandler = new CryptoAgent();

        [ActionName("NewUser")]
        public NewUserResponse PostNewUser([FromBody]NewUserRequest request)
        {
            NewUserResponse response = new NewUserResponse();

            dsComm.AddParameter("@ParamUserName", request.UserName);
            dsComm.AddParameter("@ParamPassword", securityHandler.EncryptText(request.Password));
            dsComm.AddParameter("@ParamUserType", request.UserType);
            dsComm.AddParameter("@ParamFirstName", request.FirstName);
            dsComm.AddParameter("@ParamLastName", request.LastName);
            dsComm.AddParameter("@ParamSecretQn", request.SecretQn);
            dsComm.AddParameter("@ParamSecretAns", securityHandler.EncryptText(request.SecretAns));

            int datapwd = dsComm.ExecuteNonQuery("dbo.spAddNewUser");
            if (datapwd > 0)
            {
                response.StatusMsg = "User Created";
            }
            return response;
        }
        /// <summary>
        /// Options Resposen for the preflight request of a CORS Opertaions. Will be standard across various functions
        /// </summary>
        /// <returns></returns>
        public HttpResponseMessage OptionsValidateUserLogin()
        {
            var response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.OK;
            return response;
        }

    }
}
